package com.itheima.controller;

import com.itheima.bean.Category;
import com.itheima.bean.Employee;
import com.itheima.bean.PageBean;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.exception.CustomException;
import com.itheima.service.CategoryService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tang
 * @date 2022/11/22 19:59
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类或者菜品套餐
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Category category) {
        if (category == null) {
            throw new CustomException("数据异常");
        }
        return categoryService.add(category);
    }

    /**
     * 分类分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<PageBean<Category>> findPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        return categoryService.findPage(page, pageSize);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> deleteById(@PathVariable Long id) {
        if (id == null) {
            throw new CustomException("数据异常");
        }
        return categoryService.deleteById(id);
    }

    /**
     * 点击修改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> editById(@RequestBody Category category) {
        log.info(category.toString());
        R<String> stringR = categoryService.updateById(category);
        return stringR;
    }

    /**
     * 根据tye查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> findByType(Integer type){
        if (type == null) {
            throw new CustomException("数据异常");
        }
        return categoryService.findByType(type);
    }


}
